package com.uplooking.pojo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.uplooking.constant.UserConstants;

@Entity
@Table(name="tbluser")
public class UserVO implements UserDetails {

	@Column(name="uid")
	private String uid;
	@Id
	@Column(name="username")
	private String username;
	@Column(name="password")
	private String password;
	@Column(name="status")
	private Integer status;
	@ManyToOne
	@JoinColumn(name="status",insertable=false,updatable=false)
	private DictVO dictVO;
	public DictVO getDictVO() {
		return dictVO;
	}
	public void setDictVO(DictVO dictVO) {
		this.dictVO = dictVO;
	}
	@Column(name="role")
	private String role;
	@ManyToOne
	@JoinColumn(name="role",insertable=false,updatable=false)
	private RoleVO roleVO;
	public RoleVO getRoleVO() {
		return roleVO;
	}
	public void setRoleVO(RoleVO roleVO) {
		this.roleVO = roleVO;
	}
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="username",insertable=false,updatable=false)
	private ShopVO shopVO;
	public ShopVO getShopVO() {
		return shopVO;
	}
	public void setShopVO(ShopVO shopVO) {
		this.shopVO = shopVO;
	}
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="username",insertable=false,updatable=false)
	private CustVO custVO;
	public CustVO getCustVO() {
		return custVO;
	}
	public void setCustVO(CustVO custVO) {
		this.custVO = custVO;
	}
	
	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getRole() {
		return role;
	}
	
	public void setRole(String role) {
		this.role = role;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

	public UserVO() {
		super();
	}

	public UserVO(String username, String password, Integer status, String role) {
		super();
		this.uid = UUID.randomUUID().toString();
		this.username = username;
		this.password = password;
		this.status = status;
		this.role = role;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authoritie= new ArrayList<GrantedAuthority>();
		authoritie.add(new SimpleGrantedAuthority(roleVO.getRname()));
		return authoritie;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return status!=UserConstants.UserExpireStatus;
	}

	@Override
	public boolean isAccountNonLocked() {
		return status!=UserConstants.UserLockStatus;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return status!=UserConstants.UserCheckStatus;
	}

	@Override
	public boolean isEnabled() {
		return status==UserConstants.UserCommStatus;
	}

}
